<?php

namespace App\Http\Controllers;
use App\CodeResponse;
use App\Models\Help;
use App\Models\Info;
use App\Models\Recode;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class UserController extends WxController
{

    public function editUser(Request $request)
    {
        $id = $request->user_id;
        $user_nickname = $request['nickname'];
        $pic = $request['user_img'];
        $sex = $request['sex'];
        //修改用户信息
        User::updateUser($id, $user_nickname, $pic, $sex);
        return $this->success();
    }

    //点亮
    public function light(Request $request)
    {
        $id = $request->user_id;
        $tel = $request->user->mobile;
        $mall_id = $request['mall_id'];

        if ($request->user->light_num <= 0) {
            return $this->fail(CodeResponse::FAIL, '次数不足');
        }
//        判断我点亮过没
        $info = Info::query()->where(['user_id' => $id, 'mall_id' => $mall_id])->first();
        if ($info) {
            return $this->fail(CodeResponse::FAIL, '此商城已经点亮');
        }
        //查询当前人是不是会员
        $method = 'fzapi.crm.EnqMemberInfo';
        $content = [];
        $content['telephone'] = $tel;
//        $content['mall_id'] = $mall_id;
        $time = date('Y-m-d H:i:s');
        $yxh_status = Cache::has('uid_'.$id.'_yxh_status') ? 1 : 0;
        $yhc_status = Cache::has('uid_'.$id.'_yhc_status') ? 1 : 0;
//        $yxh_status = 0;
//        $yhc_status = 0;
        if (($mall_id == 'P0299') && ($yhc_status==0)){
            $url = 'https://yhretailapi.yuexiuproperty.cn/crm/fzapi.ashx';
            $query = new Requery($method, $content, $time,$url);
            $res = $query->query($time);
           if ($res['code'] != 1000) {
                return $this->fail(CodeResponse::FAIL, '你不是当前商城会员');
            }
            if ($res['code'] == 1000 && empty($res['data'][0]['wechatOpenID'])){
                return $this->fail(CodeResponse::FAIL, '你不是当前商城会员');
            }
        }
        if($yxh_status==0){
             $query = new Requery($method, $content, $time);
             $res = $query->query($time);
             if ($res['code'] != 1000) {
                 return $this->fail(CodeResponse::FAIL, '你不是当前商城会员');
             }
            if ($res['code'] == 1000 && empty($res['data'][0]['wechatOpenID'])){
                return $this->fail(CodeResponse::FAIL, '你不是当前商城会员');
            }
        }

            //然后执行点亮
            try {
                DB::transaction(function () use ($id, $mall_id, $tel) {
                    User::query()->where('id', $id)->decrement('light_num');
                    Info::query()->insert([
                        'user_id' => $id,
                        'mall_id' => $mall_id,
                        'user_tel' => $tel,
                        'created_at' => time(),
                        'updated_at' => time(),
                    ]);
                });
                return $this->success();
            } catch (\Exception $e) {
                return $this->fail(CodeResponse::FAIL, $e->getMessage());
            }

    }

    //获取我的状态
    public function getMyStatus(Request $request)
    {
        $id = $request->user_id;
        $succ_status = [];
        $fail_status = [];
        $mall_ids = Info::query()->where('user_id', $id)->pluck('mall_id')->toArray();
        $arr = config('luck.mall_ids');
        if (!empty($mall_ids)) {
            foreach ($arr as $k => $v) {
                if (in_array($v, $mall_ids)) {
                    $succ_status[] = $v;
                } else {
                    $fail_status[] = $v;
                }
            }
        }

        return $this->success([
            'success' => $succ_status,
            'fail' => $fail_status,
        ]);
    }

    public function detail(Request $request)
    {
        $nums = $request->user->light_num;
        return $this->success([
            'light_num' => $nums
        ]);
    }

    public function share(Request $request)
    {
        $suid = (int)$request['suid'];//分享者id
        $user_id = $request->user_id;//当前人id
        //查看当前人助力没有
        $start_time =strtotime(date('Y-m-d 00:00:00'));
        $end_time =strtotime(date('Y-m-d 23:59:59') );
        $info = Help::query()->where(['user_id' => $suid, 'share_user_id' => $user_id])->whereBetween('create_time',[$start_time,$end_time])->first();
        $count = Help::query()->where(['user_id' => $suid])->whereBetween('create_time',[$start_time,$end_time])->count();
        if (!$info && ($suid != $user_id) && ($count<3)) {
           DB::transaction(function()use($suid,$user_id){
               Help::query()->insert([
                   'user_id' => $suid,
                   'share_user_id' => $user_id,
                   'create_time' => time(),
               ]);
               User::query()->where('id',$suid)->increment('light_num');
               User::query()->where('id',$user_id)->increment('light_num');
           });
        }
        return $this->success([]);
    }

    public function isFree(Request $request){
         //判断是否两个商城会员
        $method = 'fzapi.crm.EnqMemberInfo';
        $id = $request->user_id;
//        $method = 'fzapi.crm.EnquireMemberBaseInfo';
        $content = [];
        $content['telephone'] = $request->user->mobile;
//        $yxh_status = 0;
//        $yhc_status = 0;
        $yxh_status = Cache::has('uid_'.$id.'_yxh_status') ? 1 : 0;
        $yhc_status = Cache::has('uid_'.$id.'_yhc_status') ? 1 : 0;
        if ($yxh_status == 0){
//            $content['mall_id'] = '10295';
            $time = date('Y-m-d H:i:s');
            $query = new Requery($method, $content, $time);
            $res = $query->query($time);

            if ($res['code'] == 1000 && !empty($res['data'][0]['wechatOpenID'])) {
                Cache::put('uid_'.$id.'_yxh_status',1,60*24*10);
                $yxh_status = 1;
            }
        }
        if ($yhc_status == 0 ){
            $time = date('Y-m-d H:i:s');
            $url = config('luck.yhc_url');
            $query = new Requery($method, $content,$time,$url);
            $result = $query->query($time);
            if ($result['code'] == 1000 && !empty($result['data'][0]['wechatOpenID']) ) {
                Cache::put('uid_'.$id.'_yhc_status',1,60*24*10);
                $yhc_status = 1;
            }
        }
        return $this->success([
            'yxh_status'=>$yxh_status,
//            'yhc_desc'=>$result,
//            'yxh_desc'=>$res,
            'yhc_status'=>$yhc_status,
        ]);

    }

    public function incrNum(Request $request){
        $id = $request->user_id;
        //同时进行判断今天有没有登录
        $start_time =strtotime(date('Y-m-d 00:00:00'));
        $end_time =strtotime(date('Y-m-d 23:59:59') );
        $res =  Recode::query()->where('user_id',$id)->whereBetween('created_at',[$start_time,$end_time])->first();
        if (!$res){
            DB::transaction(function () use ($id){
                Recode::query()->insert([
                    'user_id'=>$id,
                    'created_at'=>time(),
                    'updated_at'=>time(),
                    'light_num'=>3
                ]);
                User::query()->where('id', $id)->increment('light_num',3);
            });
        }
        return $this->success();
    }
}